package com.roger.factory;

import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.search.Query;

import com.roger.query.service.QueryService;
import com.roger.query.service.impl.PhraseQueryServiceImpl;
import com.roger.query.service.impl.RangeQueryServiceImpl;
import com.roger.query.service.impl.SingleQueryServiceImpl;
import com.roger.query.service.impl.SpanQueryServiceImpl;
import com.roger.query.service.impl.WildcardQueryServiceImpl;
import com.roger.utils.Constant.EnumQuery;
import com.roger.vo.QueryParams;

/**
 * 生成Query的工厂
 * @author luoweiyi
 *
 */
public class QueryFactory {
	
	private Map<EnumQuery,QueryService> map;
	
	private static QueryFactory qf;
	
	private QueryFactory(){
		init();
	}
	
	/**
	 * 初始化map
	 */
	private void init(){
		map = new HashMap<EnumQuery,QueryService>();
		map.put(EnumQuery.SINGLE_QUERY, new SingleQueryServiceImpl());
		map.put(EnumQuery.SPAN_QUERY, new SpanQueryServiceImpl());
		map.put(EnumQuery.PHRASE_QUERY, new PhraseQueryServiceImpl());
		map.put(EnumQuery.RANGE_QUERY, new RangeQueryServiceImpl());
		map.put(EnumQuery.WILDCARD_QUERY, new WildcardQueryServiceImpl());
		
	}
	
	public static QueryFactory getInstance(){
		if(qf == null){
			synchronized(QueryFactory.class){
				if(qf == null){
					qf = new QueryFactory();
				}
			}
		}
		
		return qf;
	}
	
	public Query getQuery(QueryParams params,EnumQuery queryType){
		
		if(map.containsKey(queryType))
			return map.get(queryType).getQuery(params);
		
		return null;
	}

}
